<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24" ref="hMain">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入名称" clearable  prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container" :style="{height:tableHeight+'px'}" style="overflow-y: auto">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :style="style"
            :filter-node-method="filterNode" :default-expanded-keys="expandedkeys" :current-node-key="currentNodekey"
            ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick">
            <template slot-scope="{ node, data }">
              <span class="span-ellipsis" :title="node.label" :style="style">{{ node.label }}</span>
            </template>
          </el-tree>
        </div>
      </el-col>
      <!--用户数据-->
      <!-- v-hasPermi="['system:user:add']" -->
      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8" ref="refbut">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus"  @click="handleAdd"
              >添加</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="generalList" :height="tableHeight" border>
          <!-- <el-table-column type="selection" width="50" align="center" /> -->
          <el-table-column label="序号" sortable align="center" type="index" />
          <el-table-column label="全宗名称" align="center" key="fondsName" prop="fondsName" :show-overflow-tooltip="true" />
          <el-table-column label="全宗号" align="center" key="fondsCode" prop="fondsCode" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button  type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['manage:fonds:edit']">修改</el-button>
              <el-button  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['manage:fonds:remove']">删除</el-button>

            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="35%" style="margin-top:8%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="全宗名称：" prop="fondsName">
          <el-input v-model="form.fondsName" placeholder="请输入全宗名称" />
        </el-form-item>

        <el-form-item label="全宗号：" prop="fondsCode">
          <el-input v-model="form.fondsCode" placeholder="请输入全宗号" :disabled="qzhIsDisabled" onKeyUp="value=value.replace(/[\W]/g,'')"
            @blur="salaryChange" />
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTenantTree, listFONDS, addFONDS,updateFONDS,getFONDS,delFONDS} from "@/api/manage/general"
import estimateTableHeight from '@/mixins/estimateTableHeight'//vue项目中element-tabel自动计算高度
export default {
  mixins: [estimateTableHeight], // 混入方法
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 立档单位表格数据
      generalList: null,
      // 弹出层标题
      title: "",
      //立档单位信息树lieb
      deptOptions: undefined,
      //默认展开的树节点
      expandedkeys: [],
      //默认选中的树节点
      currentNodekey: "",
      //全宗号：添加可以编辑，修改时禁止编辑
      qzhIsDisabled: false,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantCode: undefined,
      },

      // 表单校验
      rules: {
        fondsName: [
          { required: true, message: "请输入全宗名称", trigger: "blur" },
        ],
        fondsCode: [
          { required: true, message: "请输入全宗号", trigger: "blur" }
        ]
      },
      style: ''
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getDeptTree();
    if (this.$store.getters.size == 'small') {
      this.style = 'font-size: 12px;'
    } else {
      this.style = 'font-size: 14px;'
    }
  },
  methods: {
    //解决vue+element— el-input 限制只能输入数字时和字母时与表单验证rules冲突问题
    salaryChange(e) {
      this.form.fondsCode = e.target.value;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listFONDS(this.queryParams).then(response => {
        this.generalList = response.rows;
        console.log(this.generalList)
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    /** 查询立档单位树结构 */
    getDeptTree() {
      listTenantTree().then(response => {
        this.deptOptions = response.data;
        if (this.deptOptions.length > 0) {
          if(this.deptOptions[0].children!=undefined ){

            this.currentNodeKey = this.deptOptions[0].children[0].id;
            this.expandedkeys.push(this.deptOptions[0].children[0].id);
            console.log(this.expandedkeys, this.currentNodeKey)
            this.$nextTick(() => {
              this.$refs.tree.setCurrentKey(this.deptOptions[0].children[0].id)
            })
            this.queryParams.tenantCode = response.data[0].children[0].tenantCode;
            this.getList();
          }
        }
        this.loading=false;

      });
    },
    // 表单重置
    reset() {
      this.form = {
        fondsName: null,
        fondsCode: null,
        tenantCode: this.queryParams.tenantCode
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      console.log(" data.tenantCode====="+ data.tenantCode);
      // if (data.tenantCode==null){
      //   this.$modal.msgWarning("请选择租户节点添加");
      //   return;
      // }
      this.queryParams.tenantCode = data.tenantCode;
      this.getList();

      this.form["tenantCode"]= data.tenantCode;
      this.getList();

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 新增按钮操作 */
    handleAdd() {
      if (this.queryParams.tenantCode==null){
           this.$modal.msgWarning("请选择租户节点添加");
           return;
      }
      this.open = true;
      this.title = "添加立档单位全宗";
      this.qzhIsDisabled = false;
      this.$refs["form"].clearValidate();
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ID = row.id || this.ids;
      getFONDS(ID).then(response => {
        this.form = response.data;
        this.open = true;
        this.qzhIsDisabled = true;
        this.$refs["form"].clearValidate();
        this.title = "修改立档单位全宗";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.form, "lsdjfkl")

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateFONDS(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFONDS(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ID = row.id || this.ids;
      this.$modal.confirm('是否确认删除全宗名称为"' + row.fondsName + '"的数据项？').then(function () {
        return delFONDS(ID);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

  }
}
</script>
